package com.vsd.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.util.JSONUtils;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.shopcart.domain.StorePhoto;
import com.vsd.service.StorePhotoService;

@Controller
@RequestMapping("storePhoto")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class StorePhotoController extends CoreAdminController{
	@Autowired
	private StorePhotoService storePhotoService;
	
	@RequestMapping("/list")
	@ResponseBody
	public String citylist(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String storeId,HttpServletRequest request){
		
		if("store".equals(getSessionParameter("ROLE_CODE", session))) storeId = getSessionParameter("id", session);
		
		
		Page<StorePhoto> list = storePhotoService.findByStoreId( storeId,new PageRequest(start/length, length, new Sort(Direction.DESC, "createTime")));
		
		return JSONUtils.serialize(new DataTable(draw+1,Integer.parseInt(list.getTotalElements()+""), list.getContent()));
	}
	
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(StorePhoto record,HttpSession session) throws Exception{
		if(StringUtils.isBlank(record.getId())){
			//是否已推荐
			record.setStoreId(getSessionParameter("id", session));
			record.setId(StringUtil.getUUID());
			record.setCreateTime(new Date());
			storePhotoService.save(record);
		}else{
			storePhotoService.updateByPrimaryKeySelective(record);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/findById")
	@ResponseBody
	public String findById(String id){
		StorePhoto r = storePhotoService.findOne(id);
		return JSONUtils.serialize(r);
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String string : ids) {
			storePhotoService.delById(string);
		}
		return JSONUtils.serialize(new Ret(0));
	}


}
